<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/

/**
 * Description of Transaksi
 *
 * @author Hadi
 */
class kas extends Admin_My_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->helper('kas_helper');
//        $this->output->enable_profiler(true);
    }

    public function kas_penjualan($is_popup = 0) {
        $this->load->model('kas/model_kas_penjualan');
        $this->load->library("datagrid");

        $data = array();

        $this->log_event = "Membuka menu kas penjualan";

        $data['srcPeriodStart'] = $this->input->get_post('srcPeriodStart') != "" ? $this->input->get_post('srcPeriodStart') : date("Y-m-d");
        $data['srcPeriodEnd'] = $this->input->get_post('srcPeriodEnd') != "" ? $this->input->get_post('srcPeriodEnd') : date("Y-m-d");

        $field_name = array("k.keterangan", "i.nama_item", "k.submitter_dp", "jkp.nama_jkp", "sjkp.nama_sjkp");
        $field_title = array("Keterangan", "Item", "NIK Submitter", "Jenis", "Sub Jenis");
        $field_type = array("text", "text", "text", "text", "text");

        $filter_panel = new FilterPanel(base_url() . "kas/kas_penjualan", $field_name, $field_title, $field_type);

        if ($filter_panel->getFilterString() == "")
            $data['srcPeriodMode'] = $this->input->get_post('srcPeriodMode') != "" ? $this->input->get_post('srcPeriodMode') : "include";
        else
            $data['srcPeriodMode'] = $this->input->get_post('srcPeriodMode') != "" ? $this->input->get_post('srcPeriodMode') : "";


        if($data['srcPeriodMode'] == 'include')
            $where = " WHERE date(k.tanggal_dp) BETWEEN '".$data['srcPeriodStart']."' AND '".$data['srcPeriodEnd']."' ";
        else
            $where = " WHERE true ";
        $filter = $filter_panel->getFilterString();
        $where .= $filter ? " AND $filter " : "";

        $offset = $this->input->get("per_page") ? $this->input->get("per_page") : 0;
        $length = 30;
        $result = $this->model_kas_penjualan->getKasPenjualanData($where, $offset, $length);
        $rowCount = $result->num_rows();

        if(!$is_popup) {
            $this->datagrid->setActionColumnsWidth(50);
            $this->datagrid->addMenu(GreyBox::createLink(base_url() . "index.php/kas/kas_penjualan_add/", "", "Tambah Data", "add", "onclick", 1024, 600));
            $this->datagrid->addMenu("<a href=\"" . base_url() . "index.php/kas/kas_penjualan_export/?" . $this->input->server("QUERY_STRING") . "\" class=\"report\" title=\"Export Data Kas Penjualan\">&nbsp;</a>");
        }
        else {
            $this->datagrid->setActionColumnVisible(false);
        }

        $this->datagrid->setColumns("No.", "Tanggal", "Keterangan", "Nama","Jenis", "Sub Jenis","Quantity", "Harga Beli", "Harga Jual", "Submitter");
        $this->datagrid->setPaginationDataNew($this->model_kas_penjualan->getKasPenjualanDataRC($where), $length, "transaksi/kas_penjualan", $rowCount, $offset);
        $this->datagrid->setButtonExportVisible(false);

        if ($rowCount > 0) {
            $nomor = $offset + 1;

            foreach ($result->result() as $row) {
                $rowID = $row->id_dp;
                if(!$is_popup) {
                    $this->datagrid->addRowAction("edit('" . $rowID . "')", "edit", "Ubah");
                    $this->datagrid->addRowAction("hapus('" . $rowID . "','".$this->input->server("QUERY_STRING")."')", "delete", "Hapus");
                } else {
                    $this->datagrid->onRowClickAction("parent.parent._obj_fSendValue('" . $rowID . "')");
                }

                $this->datagrid->addRowData(
                        $nomor++, $row->tanggal_dp, $row->keterangan, $row->nama_item, $row->nama_jkp, $row->nama_sjkp, number_format($row->quantity_dp), $row->currency." ".number_format($row->harga_beli_dp,2), $row->currency." ".number_format($row->harga_jual_dp,2), $row->nama_karyawan
                );
            }
        }
        else {
            $this->datagrid->setEmptyDataState("Tidak ada data");
        }

        $data['date_picker'] = new jDatePicker();
        $data['filter_panel'] = $filter_panel;

        if(!$is_popup) {
            $data['breadcrumb'] = $this->breadCrumb(true);
            $this->loadMenu('kas/kas_penjualan', $data);
        } else
            $this->loadMenuHeaderPopup('kas/kas_penjualan', $data);
    }

    public function kas_penjualan_add($kode) {
        $this->load->model('kas/model_kas_penjualan');
        $data = array();
        if ($this->input->get_post('vSave')) {
            $vars = array(
                    "tanggal_dp" => date("Y-m-d"),
                    "id_item" => "",
                    "nama_item" => "",
                    "quantity_dp" => "",
                    "currency" => "",
                    "diskon_dp"=> "",
                    "keterangan"=> "",
                    "id_sjkp"=> "",
                    "submitter_dp" => $this->userauth->getUserNIK(),
                    "date_created_dp" => date("Y-m-d H:i:s")
            );
            $data = array();
            retrieve_request($data, $vars);
            $data["harga_beli_dp"] = $this->model_kas_penjualan->getHargaItem($data["id_item"], "harga_beli", $data["currency"]);
            $data["harga_jual_dp"] = $this->model_kas_penjualan->getHargaItem($data["id_item"], "harga_jual", $data["currency"]);

            if($kode == "") {
                $id_kas = $this->model_kas_penjualan->insertKasPenjualan($data);
            } else {
                $id_kas = $kode;
                $this->model_kas_penjualan->updateKasPenjualan($data, $id_kas);
            }
            echo "<script>" . CloseGB(true) . "</script>";
        }

        if($kode != "") {
            retrieve_kas_penjualan_data ($data, $kode);
            $data["kode"] = $kode;
        } else {
            $data["tanggal_dp"] = date("Y-m-d");
        }

        $data["userid"] =$this->userauth->getUserID();
        $data['gbitem'] = $this->greybox->createLink(base_url() . 'index.php/popup/singgle_item', '<span>Tambah Item</span>', 'Pilih Item', 'find', '', 750, 450);
        $data['gbjeniskaspenjualan'] = $this->greybox->createLink(base_url() . 'index.php/popup/jenis_kas_penjualan', '<span>Pilih Jenis Kas</span>', 'Pilih Jenis Kas', 'find', '', 750, 450);

        $this->loadMenuHeaderPopup("kas/kas_penjualan_add",$data);
    }

    public function kas_penjualan_delete($kode = "") {
        $this->load->model('kas/model_kas_penjualan');
        if ($kode != null) {
            $this->model_kas_penjualan->removeDataKasPenjualan($kode);

            $this->log_event = "Menghapus Data Kas Penjualan dengan ID " . $kode;

            redirect(base_url() . 'index.php/kas/kas_penjualan?'.$this->input->server("QUERY_STRING"), 'refresh');
        }
    }

    public function kas_penjualan_export() {
        $this->load->model('kas/model_kas_penjualan');
        $data['srcPeriodStart'] = $this->input->get_post('srcPeriodStart') != "" ? $this->input->get_post('srcPeriodStart') : date("Y-m-d");
        $data['srcPeriodEnd'] = $this->input->get_post('srcPeriodEnd') != "" ? $this->input->get_post('srcPeriodEnd') : date("Y-m-d");
        $data['srcPeriodMode'] = $this->input->get_post('srcPeriodMode') != "" ? $this->input->get_post('srcPeriodMode') : "include";

        $field_name = array("k.keterangan", "i.nama_item", "k.submitter_dp", "jkp.nama_jkp", "sjkp.nama_sjkp");
        $field_title = array("Keterangan", "Item", "NIK Submitter", "Jenis", "Sub Jenis");
        $field_type = array("text", "text", "text", "text", "text");

        $filter_panel = new FilterPanel(base_url() . "kas/kas_penjualan", $field_name, $field_title, $field_type);

        if ($filter_panel->getFilterString() == "")
            $data['srcPeriodMode'] = $this->input->get_post('srcPeriodMode') != "" ? $this->input->get_post('srcPeriodMode') : "include";
        else
            $data['srcPeriodMode'] = $this->input->get_post('srcPeriodMode') != "" ? $this->input->get_post('srcPeriodMode') : "";


        if($data['srcPeriodMode'] == 'include')
            $where = " WHERE date(k.tanggal_dp) BETWEEN '".$data['srcPeriodStart']."' AND '".$data['srcPeriodEnd']."' ";
        else
            $where = " WHERE true ";


        $queryExport = $this->model_kas_penjualan->getQueryKasPenjualan($where, 0, 0);


        $this->export($queryExport);
    }

    public function kas_bank($kode="") {
        $this->load->model('kas/kas_bank');

        $data = array();
        $data['breadcrumb'] = $this->breadCrumb(true);
        $data['srcPeriodStart'] = $this->input->get_post('srcPeriodStart') == "" ? "01-".date("m-Y") : $this->input->get_post('srcPeriodStart');
        $data['srcPeriodEnd'] = $this->input->get_post('srcPeriodEnd') == "" ? date("d-m-Y") : $this->input->get_post('srcPeriodEnd');
        $data['bank'] = $kode;

        $field_name = array("a.keterangan_kb", "a.id_kb", "ROUND(a.kas_masuk_kb)", "ROUND(a.kas_keluar_kb)", "c.nama");
        $field_title = array("Keterangan", "Id", "Kas Masuk", "Kas Keluar", "Nama Subimtter");
        $field_type = array("text", "integer", "double", "double", "text");

        $filter_panel = new FilterPanel(base_url() . 'index.php/kas/kas_bank/', $field_name, $field_title, $field_type);
        $whr = $filter_panel->getFilterString();

        $whr = " WHERE a.id_bank=".$kode;
        $whr .= " AND date(a.tanggal_kb) BETWEEN '" . unix_to_human_simple($data['srcPeriodStart']) . "' AND '" . unix_to_human_simple($data['srcPeriodEnd']) . "' ";

        $data['post'] = $this->getPostInfo();

        $data['gb'] = $this->greybox->createLink(base_url() . 'index.php/management/karyawan_edit', '', 'Tambah Data', 'add');
        $data['dp'] = new jDatePicker();
        $data['fp'] = $filter_panel->drawfilterpanel();
        $data['fp2'] = $filter_panel->drawfilterpanelsingle();
        
        $this->kas_bank->initSaldo($whr, 30, $data['post']['offset'],unix_to_human_simple($data['srcPeriodStart']));        
        $data['resultSetAll'] = $this->kas_bank->getAllKasBank($whr);        
        $data['resultSet'] = $this->kas_bank->getKasBank($whr, 30, $data['post']['offset']);
        
        $whrsal = " WHERE a.id_bank=".$kode;        
        $data['resultSetSaldo'] = $this->kas_bank->getSaldo($whrsal);
        
        $whrsal = " WHERE a.id_bank=".$kode;
        $whrsal .= " AND date(a.tanggal_kb) < '" . date("Y-m", strtotime($data['srcPeriodStart'])) . "-01' ";        
        $data['resultSetSaldoAwalBulan'] = $this->kas_bank->getSaldoAwalBulan($whrsal);
        
        $data['result'] = $data['resultSet']->result();
        $data['resultSaldo'] = $data['resultSetSaldo']->result();
        $data['resultSaldoAwalBulan'] = $data['resultSetSaldoAwalBulan']->result();
        $data['bulan'] = date("M", strtotime($data['srcPeriodStart']));
        $data['numRows'] = $data['resultSet']->num_rows();
        $data['numRowsSaldo'] = $data['resultSetSaldo']->num_rows();
        $data['numRowsSaldoAwalBulan'] = $data['resultSetSaldoAwalBulan']->num_rows();

        $data['paging'] = $this->MakePagingPage($data['resultSetAll']['last_query'], $data['post']['offset'], $data['post']['colPage'], $data['post']['nextPage']);
        $_SESSION[USER_AUTH . 'last_query'] = $data['resultSetAll']['last_query'];

        $this->log_event = "Membuka menu data kas bank";

        $this->loadMenu('/kas/kas_bank', $data);
    }

    public function kas_bank_delete($kode = "", $bank = "", $query = "") {
        $this->load->model('kas/kas_bank');

        if ($kode != null) {
            $this->kas_bank->removeKasBank($kode);

            $this->log_event = "Menghapus Data Kas Bank dengan id " . $kode;

            header("location:" . base_url() . 'index.php/kas/kas_bank/'.$bank.'?' . urldecode($query));
        }
    }

    public function kas_bank_edit($kode,$bank) {
        $this->load->model('/kas/kas_bank');

        $data = array();

        $data['tanggal_kb'] = $this->input->get_post('tanggal_kb');
        $data['kas_masuk_kb'] = trim($this->input->get_post('kas_masuk_kb'));
        $data['kas_keluar_kb'] = trim($this->input->get_post('kas_keluar_kb'));
        $data['keterangan'] = $this->input->get_post('keterangan');
        $data['id_bank'] = $this->input->get_post('id_bank');
        $data['submitter_kb'] = $this->general_model->getScalar('nik', 'mt_user', 'id_user', $this->userauth->getUserID());

        $data['vSave'] = $this->input->get_post('vSave');
        $data['vMode'] = $this->input->get_post('vMode');
        $data['kode'] = $kode;
        $data['msg'] = "";

        if ($data['vSave']) {
            if ($data['tanggal_kb'] == "") {
                $data['msg'] = "Tanggal harus diisi!";
            } else if ($data['id_bank'] == "") {
                $data['msg'] = "Nama bank harus dipilih!";

            } else {
                $arrVals = array(
                        "tanggal_kb" => $data['tanggal_kb'],
                        "kas_masuk_kb" => $data['kas_masuk_kb'],
                        "kas_keluar_kb" => $data['kas_keluar_kb'],
                        "keterangan_kb" => $data['keterangan'],
                        "submitter_kb" => $data['submitter_kb'],
                        "id_bank" => $data['id_bank'],
                        "date_created_kb" => date("Y-m-d H:i:s")
                );

                if ($data['vMode'] == "add") {
                    $this->kas_bank->addKasBank($arrVals);

                    $this->log_event = "Menambah data kas bank";

                    echo $this->db->last_query();
                    echo "<script>" . CloseGB(true) . "</scr" . "ipt>";
                } else {
                    $this->kas_bank->updateKasBank($data['kode'], $arrVals);

                    $this->log_event = "Merubah data kas bank dengan id $data[kode]";
                    echo $this->db->last_query();

                    echo "<script>" . CloseGB(true) . "</scr" . "ipt>";
                }
            }
        } else {
            if ($data['kode'] != "") {
                $resultSet = $this->kas_bank->getKasBankDetil($data['kode']);
                $row = $resultSet->row();

                $data['tanggal_kb'] = $row->tanggal_kb;
                $data['kas_masuk_kb'] = $row->kas_masuk_kb;
                $data['kas_keluar_kb'] = $row->kas_keluar_kb;
                $data['keterangan'] = $row->keterangan_kb;
            }
        }

        $data['gbbank'] = $this->greybox->createLink(base_url() . 'index.php/popup/bank', '<span>Cari</span>', 'Cari Bank', 'find', '', 750, 450);
        $data['id_bank'] = $bank;
        $data['nama_bank'] = $this->general_model->getScalar('nama_bank', 'master_bank', 'id_bank', $bank);

        $this->loadMenuHeaderPopup('/kas/kas_bank_edit', $data);
    }

    public function kas_bank_export($bank) {
        $this->load->model('/kas/kas_bank');
        
        $data['srcPeriodStart'] = $this->input->get_post('srcPeriodStart') == "" ? date("d-m-Y") : $this->input->get_post('srcPeriodStart');
        $data['srcPeriodEnd'] = $this->input->get_post('srcPeriodEnd') == "" ? date("d-m-Y") : $this->input->get_post('srcPeriodEnd');

        $field_name = array("a.id_kb", "a.kas_masuk_kb", "a.kas_keluar_kb", "a.keterangan_kb", "c.nama");
        $field_title = array("Id", "Kas Masuk", "Kas Keluar", "Keterangan", "Nama Subimtter");
        $field_type = array("integer", "integer", "integer", "text", "text");

        $filter_panel = new FilterPanel(base_url() . 'index.php/kas/kas_bank', $field_name, $field_title, $field_type);
        $whr = $filter_panel->getFilterString();

        $whr = " WHERE a.id_bank=".$bank;
        $whr .= " AND date(a.tanggal_kb) BETWEEN '" . unix_to_human_simple($data['srcPeriodStart']) . "' AND '" . unix_to_human_simple($data['srcPeriodEnd']) . "' ";

        $this->kas_bank->initSaldo($whr, 0, 0,unix_to_human_simple($data['srcPeriodStart']));        
        $queryExport = "SELECT a.tanggal_kb as tanggal,
        a.keterangan_kb as keterangan, 
        @km_kb := a.kas_masuk_kb as kas_masuk, @kk_kb := a.kas_keluar_kb as kas_keluar, @saldo_kb := @saldo_kb + @km_kb - @kk_kb AS saldo,
        b.nama_bank, c.nama as submitter FROM kas_bank a 
        LEFT JOIN master_bank b on (a.id_bank=b.id_bank) 
        LEFT JOIN mt_karyawan c on (a.submitter_kb=c.nik) 
                $whr";
        $this->export($queryExport,"kas_bank");
    }

    public function kas_operasional($is_popup = 0) {
        $this->load->model('kas/model_kas_operasional');
        $this->load->library("datagrid");

        $data = array();

        $this->log_event = "Membuka menu kas operasional";

        $data['srcPeriodStart'] = $this->input->get_post('srcPeriodStart') != "" ? $this->input->get_post('srcPeriodStart') : date("Y-m-")."01";
        $data['srcPeriodEnd'] = $this->input->get_post('srcPeriodEnd') != "" ? $this->input->get_post('srcPeriodEnd') : date("Y-m-d");
        
        $field_name = array("k.keterangan_ko", "k.bukti_kas", "k.submitter_ko", "jko.nama_jko", "sjko.nama_sjko");
        $field_title = array("Keterangan", "Bukti Kas","NIK Submitter", "Jenis", "Sub Jenis");
        $field_type = array("text", "text", "text", "text", "text");

        $filter_panel = new FilterPanel(base_url() . "kas/kas_operasional", $field_name, $field_title, $field_type);

        if ($filter_panel->getFilterString() == "")
            $data['srcPeriodMode'] = $this->input->get_post('srcPeriodMode') != "" ? $this->input->get_post('srcPeriodMode') : "include";
        else
            $data['srcPeriodMode'] = $this->input->get_post('srcPeriodMode') != "" ? $this->input->get_post('srcPeriodMode') : "";


        if($data['srcPeriodMode'] == 'include')
            $where = " WHERE date(k.tanggal_ko) BETWEEN '".$data['srcPeriodStart']."' AND '".$data['srcPeriodEnd']."' ";
        else
            $where = " WHERE true ";
        $filter = $filter_panel->getFilterString();
        $where .= $filter ? " AND $filter " : "";

        $offset = $this->input->get("per_page") ? $this->input->get("per_page") : 0;
        $length = 30;
        
        $this->model_kas_operasional->initSaldo($where, $offset, $length, $data['srcPeriodStart']);
        
        $data['saldo'] = $this->model_kas_operasional->getSaldoKasOperasional();        
        $data['saldo_awal_bulan'] = $this->model_kas_operasional->getSaldoKasOperasionalSaatIni($data['srcPeriodStart']);
        $data['bulan'] = date("M", strtotime($data['srcPeriodStart']));
        
        $result = $this->model_kas_operasional->getKasOperasionalData($where, $offset, $length);
        $rowCount = $result->num_rows();

        if(!$is_popup) {
            $this->datagrid->setActionColumnsWidth(50);
            $this->datagrid->addMenu(GreyBox::createLink(base_url() . "index.php/kas/kas_operasional_add/", "", "Tambah Data", "add", "onclick", 1024, 600));
            $this->datagrid->addMenu("<a href=\"" . base_url() . "index.php/kas/kas_operasional_export/?" . $this->input->server("QUERY_STRING") . "\" class=\"report\" title=\"Export Data Kas Operasional\">&nbsp;</a>");
        }
        else {
            $this->datagrid->setActionColumnVisible(false);
        }
                        
        $this->datagrid->setColumns("No.", "Tanggal", "Keterangan", "Bukti Kas","Kas Masuk", "Kas Keluar", "Saldo");
        $this->datagrid->setPaginationDataNew($this->model_kas_operasional->getKasOperasionalDataRC($where), $length, "kas/kas_operasional", $rowCount, $offset);
        $this->datagrid->setButtonExportVisible(false);

        if ($rowCount > 0) {
            $nomor = $offset + 1;

            foreach ($result->result() as $row) {
                $rowID = $row->id_ko;
                if(!$is_popup) {
                    $this->datagrid->addRowAction("edit('" . $rowID . "')", "edit", "Ubah");
                    $this->datagrid->addRowAction("hapus('" . $rowID . "','".$this->input->server("QUERY_STRING")."')", "delete", "Hapus");
                } else {
                    $this->datagrid->onRowClickAction("parent.parent._obj_fSendValue('" . $rowID . "')");
                }

                $this->datagrid->addRowData(
                        $nomor++, $row->tanggal_ko, $row->keterangan_ko, $row->bukti_kas, number_format($row->kas_masuk_ko), number_format($row->kas_keluar_ko), number_format($row->saldo)
                );
            }
        }
        else {
            $this->datagrid->setEmptyDataState("Tidak ada data");
        }

        $data['date_picker'] = new jDatePicker();
        $data['filter_panel'] = $filter_panel;

        if(!$is_popup) {
            $data['breadcrumb'] = $this->breadCrumb(true);
            $this->loadMenu('kas/kas_operasional', $data);
        } else
            $this->loadMenuHeaderPopup('kas/kas_operasional', $data);
    }

    public function kas_operasional_add($kode) {
        $this->load->model('kas/model_kas_operasional');
        $data = array();
        if ($this->input->get_post('vSave')) {
            $vars = array(
                    "tanggal_ko" => date("Y-m-d"),
                    "kas_masuk_ko" => "",
                    "kas_keluar_ko" => "",
                    "keterangan_ko" => "",
                    "bukti_kas" => "",
                    "id_sjko" => "",
                    "submitter_ko" => $this->userauth->getUserNIK(),
                    "date_created_ko" => date("Y-m-d H:i:s")
            );
            $data = array();
            retrieve_request($data, $vars);

            if($kode == "") {
                $id_kas = $this->model_kas_operasional->insertKasOperasional($data);
            } else {
                $id_kas = $kode;
                $this->model_kas_operasional->updateKasOperasional($data, $id_kas);
            }
            echo "<script>" . CloseGB(true) . "</script>";
        }

        if($kode != "") {
            retrieve_kas_operasional_data ($data, $kode);
            $data["kode"] = $kode;
        } else {
            $data["tanggal_ko"] = date("Y-m-d");
        }


        $data["userid"] =$this->userauth->getUserID();
        $data['gbjeniskasoperasional'] = $this->greybox->createLink(base_url() . 'index.php/popup/jenis_kas_operasional', '<span>Pilih Jenis Kas</span>', 'Pilih Jenis Kas', 'find', '', 750, 450);

        $this->loadMenuHeaderPopup("kas/kas_operasional_add",$data);
    }

    public function kas_operasional_delete($kode = "") {
        $this->load->model('kas/model_kas_operasional');
        if ($kode != null) {
            $this->model_kas_operasional->removeDataKasOperasional($kode);

            $this->log_event = "Menghapus Data Kas Operasional dengan ID " . $kode;

            redirect(base_url() . 'index.php/kas/kas_operasional?'.$this->input->server("QUERY_STRING"), 'refresh');
        }
    }

    public function kas_operasional_export() {
        $this->load->model('kas/model_kas_operasional');
        $data['srcPeriodStart'] = $this->input->get_post('srcPeriodStart') != "" ? $this->input->get_post('srcPeriodStart') : date("Y-m-d");
        $data['srcPeriodEnd'] = $this->input->get_post('srcPeriodEnd') != "" ? $this->input->get_post('srcPeriodEnd') : date("Y-m-d");
        $data['srcPeriodMode'] = $this->input->get_post('srcPeriodMode') != "" ? $this->input->get_post('srcPeriodMode') : "include";

        $field_name = array("k.keterangan_ko", "k.bukti_kas", "k.submitter_ko", "jko.nama_jko", "sjko.nama_sjko");
        $field_title = array("Keterangan", "Bukti Kas","NIK Submitter", "Jenis", "Sub Jenis");
        $field_type = array("text", "text", "text", "text", "text");

        $filter_panel = new FilterPanel(base_url() . "kas/kas_operasional", $field_name, $field_title, $field_type);

        if ($filter_panel->getFilterString() == "")
            $data['srcPeriodMode'] = $this->input->get_post('srcPeriodMode') != "" ? $this->input->get_post('srcPeriodMode') : "include";
        else
            $data['srcPeriodMode'] = $this->input->get_post('srcPeriodMode') != "" ? $this->input->get_post('srcPeriodMode') : "";

        if($data['srcPeriodMode'] == 'include')
            $where = " WHERE date(k.tanggal_ko) BETWEEN '".$data['srcPeriodStart']."' AND '".$data['srcPeriodEnd']."' ";
        else
            $where = " WHERE true ";

        $this->model_kas_operasional->initSaldo($where, 0, 0, $data['srcPeriodStart']);
        $queryExport = $this->model_kas_operasional->getQueryKasOperasional($where, 0, 0, false, true);


        $this->export($queryExport);
    }

    public function finalisasi_invoice($id_invoice, $source_kas = "invoice_cust") {
        $this->load->model('kas/model_kas_penjualan');
        $this->load->model('kas/model_kas_operasional');
        $this->load->model('transaksi/model_transaksi');
        $this->load->model('transaksi/model_transaksi_internal');
        $this->load->model('transaksi/model_transaksi_penyewaan_invoice');
        $data = array();
        if ($this->input->get_post('vSave')) {
            $vars = array(
                    "kode" => "",
                    "tanggal_dp" => date("Y-m-d"),
                    "id_item" => array(),
                    "nama_item" => array(),
                    "id_sjkp" => "",
                    "keterangan" => "",
                    "id_sjko" => "",
                    "quantity" => array(),
                    "harga_beli" => array(),
                    "harga_jual"=> array()
            );
            $data = array();
            retrieve_request($data, $vars);

            $i = 0;
            foreach($data["id_item"] as $key) {
                if ($source_kas == "invoice_cust") {
                    $varData = array(
                            "tanggal_dp" => $data["tanggal_dp"],
                            "id_item" => $key,
                            "nama_item"=> $data["nama_item"][$i],
                            "quantity_dp" => $data["quantity"][$i],
                            "currency" => "Rp",                            
                            "diskon_dp"=> "0",            
                            "keterangan" => $data["keterangan"],
                            "id_sjkp"=> $data["id_sjkp"],
                            "submitter_dp" => $this->userauth->getUserNIK(),
                            "date_created_dp" => date("Y-m-d H:i:s"),
                            "harga_beli_dp" => $data["harga_beli"][$i],
                            "harga_jual_dp" => $data["harga_jual"][$i],
                            "reff_tabel" => "data_invoice_to_cust",
                            "reff_id" => $data["kode"]
                    );
                    $this->model_kas_penjualan->insertKasPenjualan($varData);
                    $this->model_transaksi->disabledFinalisasi($data["kode"]);
                } else if ($source_kas == "invoice_supp") {
                    $vars = array(
                            "tanggal_ko" => $data["tanggal_dp"],
                            "kas_masuk_ko" => "",
                            "kas_keluar_ko" => $data["harga_beli"][$i],
                            "keterangan_ko" => $data["nama_item"][$i],
                            "id_sjko" => $data["id_sjko"],
                            "submitter_ko" => $this->userauth->getUserNIK(),
                            "date_created_ko" => date("Y-m-d H:i:s"),
                            "reff_tabel" => "data_invoice_from_supplier",
                            "reff_id" => $data["kode"]
                    );
                    $this->model_kas_operasional->insertKasOperasional($vars);
                    $this->model_transaksi_internal->disabledFinalisasi($data["kode"]);
                } else if ($source_kas == "perjanjian") {
                    $varData = array(
                            "tanggal_dp" => $data["tanggal_dp"],
                            "id_item" => $key,
                            "nama_item"=> $data["nama_item"][$i],
                            "quantity_dp" => $data["quantity"][$i],
                            "currency" => "Rp",
                            "diskon_dp"=> "0",
                            "keterangan" => $data["keterangan"],
                            "id_sjkp"=> $data["id_sjkp"],
                            "submitter_dp" => $this->userauth->getUserNIK(),
                            "date_created_dp" => date("Y-m-d H:i:s"),
                            "harga_beli_dp" => $data["harga_beli"][$i],
                            "harga_jual_dp" => $data["harga_jual"][$i],
                            "reff_tabel" => "data_invoice_to_cust_perjanjian",
                            "reff_id" => $data["kode"]
                    );
                    $this->model_kas_penjualan->insertKasPenjualan($varData);
                    $this->model_transaksi_penyewaan_invoice->disabledFinalisasi($data["kode"]);
                } 
                $i++;
            }
            echo "<script>" . CloseGB(true) . "</script>";
        } else {
            $data["tanggal_dp"] = date("Y-m-d");
            $data["kode"] = $id_invoice;

            if ($source_kas == "invoice_cust") {
                $data['item'] = $this->model_kas_penjualan->getItemInvoice($id_invoice);
                $data['diskon_invoice'] = $this->model_kas_penjualan->getDiskonInvoice($id_invoice);
                $data['gbjeniskaspenjualan'] = $this->greybox->createLink(base_url() . 'index.php/popup/jenis_kas_penjualan', '<span>Pilih Jenis Kas</span>', 'Pilih Jenis Kas', 'find', '', 750, 450);
                $data['nama_jenis'] = 'nama_sjkp';
                $data['id_jenis'] = 'id_sjkp';
                $data['source_kas'] = "invoice_cust";
            } else if ($source_kas == "invoice_supp") {                
                $data['item'] = $this->model_transaksi_internal->getItemInvoice($id_invoice);
                $data['diskon_invoice'] = 0;
                $data['source_kas'] = $source_kas;
                $data['gbjeniskaspenjualan'] = $this->greybox->createLink(base_url() . 'index.php/popup/jenis_kas_operasional', '<span>Pilih Jenis Kas</span>', 'Pilih Jenis Kas', 'find', '', 750, 450);
                $data['nama_jenis'] = 'nama_sjko';
                $data['id_jenis'] = 'id_sjko';
            } else if ($source_kas == "perjanjian") {
                $data['item'] = $this->model_kas_penjualan->getItemInvoicePerjanjian($id_invoice);
                $data['diskon_invoice'] = 0;
                $data['gbjeniskaspenjualan'] = $this->greybox->createLink(base_url() . 'index.php/popup/jenis_kas_penjualan', '<span>Pilih Jenis Kas</span>', 'Pilih Jenis Kas', 'find', '', 750, 450);
                $data['nama_jenis'] = 'nama_sjkp';
                $data['id_jenis'] = 'id_sjkp';
                $data['source_kas'] = "perjanjian";
            }


            $data['gbitem'] = $this->greybox->createLink(base_url() . 'index.php/popup/item_finalisasi', '<span>Tambah Item</span>', 'Tambah Item', 'find', '', 1024, 450);
            $this->loadMenuHeaderPopup("kas/finalisasi_invoice",$data);
        }
    }

}

?>